package com.atguigu.gmall.product.rpc;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.model.to.BaseCategroysTo;
import com.atguigu.gmall.product.service.BaseCategoryService;
import com.atguigu.gmall.common.config.cache.MyCacheService;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/9/10 6:29
 */
@RestController
@RequestMapping("/rpc/inner/product")
public class CategoryRpcController {
    @Autowired
    BaseCategoryService baseCategoryService;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    MyCacheService myCacheService;

    @GetMapping("/categorys")
    public Result<List<BaseCategroysTo>> getCategorys() {


        List<BaseCategroysTo> baseCategroysTos = baseCategoryService.getCategorys();

        //缓存不为空,直接返回
        return Result.ok(baseCategroysTos);
    }

    @GetMapping("/category/view/{skuId}")
    public Result<BaseCategoryView> getBaseCategoryView(@PathVariable("skuId") Long skuId) {
        BaseCategoryView baseCategoryView = baseCategoryService.getBaseCategoryView(skuId);
        return Result.ok(baseCategoryView);
    }

    //2.查sku信息
    @GetMapping("/skuInfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService.getById(skuId);
        QueryWrapper<SkuImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id", skuId);
        List<SkuImage> list = skuImageService.list(queryWrapper);
        skuInfo.setSkuImageList(list);
        return Result.ok(skuInfo);
    }

    //3.查价格
    @GetMapping("/price/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId") Long skuId) {

        BigDecimal price = skuInfoService.getPrice(skuId);
        return Result.ok(price);
    }

    //4.查所有销售属性集合
    @GetMapping("/skuInfo/spu/spusaleattrvalues/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttrValues(@PathVariable("skuId") Long skuId) {

        List<SpuSaleAttr> spuSaleAttrValues = skuInfoService.getSpuSaleAttrValues(skuId);
        return Result.ok(spuSaleAttrValues);
    }

    //5.查实际sku组合
    @GetMapping("/skuInfo/spu//{skuId}")
    public Result<Map<String, String>> getSkuValueJson(@PathVariable("skuId") Long skuId) {

        Map<String, String> map = skuInfoService.getSkuValueJson(skuId);
        return Result.ok(map);
    }

}
